﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using fuzhu;
using Dapper;
using LKU8.shoukuan.Model;

namespace LKU8.shoukuan
{
    public class SCJH_MorderService
    {
       

       /// <summary>
        /// 读取U8生产订单的存货编码
       /// </summary>
        /// <param name="cQianZhui">存货编码</param>
       /// <param name="constr"></param>
       /// <returns></returns> 
        public static List<InvLoopUpEntity> GetMoInvList(string _MoCode,string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            { 
                //只取末级，and bDepEnd = 1。 查询条件里，不需要这个
                string sql = @"select  cInvCode 存货编码,cInvName 存货名称,cInvStd 规格型号 
from mom_order a,mom_orderdetail b,inventory c 
where a.moid = b.MoId and b.invcode=c.cinvcode
and a.mocode=@MoCode";
                List<InvLoopUpEntity> lstMO = conn.Query<InvLoopUpEntity>(sql, new { MoCode = _MoCode }).ToList();
                return lstMO;
            }
        
        }


        /// <summary>
        /// 读取生产订单列表
       /// </summary>
        /// <param name="_MoCode">生产订单号码</param>
       /// <param name="constr"></param>
       /// <returns></returns> 
        public static List<MomOrder_Entity> GetMoList(string _MoCode,string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            { 
                //只取末级，and bDepEnd = 1。 查询条件里，不需要这个
                string sql = @" select MoDId, Qty,InvCode,a.MoCode,b.Status from mom_order a
inner join  mom_orderdetail b on a.MoId = b.MoId 
where a.MoCode=@MoCode";
                List<MomOrder_Entity> lstMO = conn.Query<MomOrder_Entity>(sql, new { MoCode = _MoCode }).ToList();
                return lstMO;
            }
        
        }

        

        /// <summary>
        /// 读取U8生产订单的数量，整数型
        /// </summary>
        /// <param name="cQianZhui">存货编码</param>
        /// <param name="constr"></param>
        /// <returns></returns> 
        public static int GetMoQty(WanJi_SCJH_View entity, string constr)
        {
            using (SqlConnection conn = new SqlConnection(constr))
            {
                //只取末级，and bDepEnd = 1。 查询条件里，不需要这个
                string sql = @"select Qty sl 
from mom_order a,mom_orderdetail b,inventory c 
where a.moid = b.MoId and b.invcode=c.cinvcode
and a.mocode=@MoCode and cInvCode= @cInvCode";
                var  qty = conn.Query<int>(sql, entity);
                if (qty != null)
                {
                    return qty.FirstOrDefault();
                }
                else
                {
                    return 0; 
               } 
            }

        }
         

       }
 
}
